﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace td.Core.DiscoverAddins
{
    public partial class ToolButtonsLists : DevExpress.XtraEditors.XtraUserControl
    {
        td.Core.Xml.AddIn addin;
        public ToolButtonsLists(td.Core.Xml.AddIn addin)
        {
            InitializeComponent();

            this.addin = addin;
            this.listBoxControl1.SelectedIndexChanged += new EventHandler(listBoxControl1_SelectedIndexChanged);
            this.UpDateListInfo();
        }
        void UpDateListInfo()
        {
            this.listBoxControl1.Items.Clear();
            for (int i = 0; i < addin.ToolButtons.Count; i++)
            {
                this.listBoxControl1.Items.Add(addin.ToolButtons[i].Caption);
            }
        }

        void listBoxControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            SetValue();
        }

        private void SetValue()
        {
            if (this.listBoxControl1.Items.Count <= 0) return;
            var t = addin.ToolButtons[this.listBoxControl1.SelectedIndex];
            this.tbCaption.Text = t.Caption;
            this.tbClass.Text = t.Class;
            this.tbGroupName.Text = t.GroupName;
            this.tbLargeImage.Text = t.largeImage;
            this.tbPageName.Text = t.PageName;
            this.tbShortCut.Text = t.ShortCut;
            this.tbSmallImage.Text = t.smallImage;
            this.tbToolTip.Text = t.ToolTip;
            if (t.ShowInNavBar != null)
            {
                this.cbShowInNavBar.EditValue = t.ShowInNavBar.ToLower() == "true" ? true : false;
            }
        }
        //新建按钮
        private void simpleButton4_Click(object sender, EventArgs e)
        {
            ClearInfo();
        }

        private void ClearInfo()
        {
            this.tbCaption.Text = "";
            this.tbClass.Text = "";
            this.tbGroupName.Text = "";
            this.tbLargeImage.Text = "";
            this.tbPageName.Text = "";
            this.tbShortCut.Text = "";
            this.tbSmallImage.Text = "";
            this.tbToolTip.Text = "";
            this.cbShowInNavBar.EditValue = false;
        }
        /// <summary>
        /// 修改保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void sbSave_Click(object sender, EventArgs e)
        {
            addin.ToolButtons[this.listBoxControl1.SelectedIndex] = SaveValue();
            this.UpDateListInfo();
        }

        private td.Core.Xml.ToolButtonXml SaveValue()
        {
            td.Core.Xml.ToolButtonXml t = new Xml.ToolButtonXml();
            t.Caption = this.tbCaption.Text;
            t.Class = this.tbClass.Text;
            t.GroupName = this.tbGroupName.Text;
            t.largeImage = this.tbLargeImage.Text;
            t.PageName = this.tbPageName.Text;
            t.ShortCut = this.tbShortCut.Text;
            t.smallImage = this.tbSmallImage.Text;
            t.ToolTip = this.tbToolTip.Text;
            t.ShowInNavBar = this.cbShowInNavBar.EditValue.ToString();
            return t;
        }

        private void sbNew_Click(object sender, EventArgs e)
        {
            addin.ToolButtons.Add(SaveValue());            
            UpDateListInfo();
        }

        private void simpleButton3_Click(object sender, EventArgs e)
        {
           
            Reflection r = new Reflection(addin);
           
          
            if (r.ShowDialog() == DialogResult.OK)
            {
                this.tbClass.Text = r.Result;
            }

        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void sbDel_Click(object sender, EventArgs e)
        {
            if (this.listBoxControl1.SelectedItem != null)
            {
                addin.ToolButtons.RemoveAt(this.listBoxControl1.SelectedIndex);
                UpDateListInfo();
            }
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            open.Filter = "*.png|*.png|*.jpg|*.jpg|*.bmp|*.bmp";
            if (open.ShowDialog() == DialogResult.OK)
            {
                this.tbLargeImage.Text = open.FileName.Replace(Application.StartupPath + "\\Images\\", "");
            }
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {

            OpenFileDialog open = new OpenFileDialog();
            open.Filter = "*.png|*.png|*.jpg|*.jpg|*.bmp|*.bmp";
            if (open.ShowDialog() == DialogResult.OK)
            {
                this.tbSmallImage.Text = open.FileName.Replace(Application.StartupPath + "\\Images\\", "");
            }
        }
    }
}
